//
//  BaseTableView.m
//  Weibo
//
//  Created by taoshanchang on 15/1/30.
//  Copyright (c) 2015年 taoshanchang. All rights reserved.
//

#import "BaseTableView.h"
#import "CONSTS.h"
#import "UIViewExt.h"

@implementation BaseTableView

-(id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style{
    self = [super initWithFrame:frame style:style];
    if(self){
        [self _initView];
    
    }
    return self;
}
//适用xib创建的时候调用
-(void)awakeFromNib{
    [self _initView];
}

-(void) _initView{
    
    _refreshHeaderView = [[EGORefreshTableHeaderView alloc] initWithFrame:CGRectMake(0.0f, 0.0f - self.bounds.size.height, self.frame.size.width, self.bounds.size.height)];
    _refreshHeaderView.delegate = self;
    _refreshHeaderView.backgroundColor = [UIColor clearColor];
   
    self.dataSource = self;
    self.delegate = self;
    self.refreshHeader = YES;

    _moreButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _moreButton.backgroundColor = [UIColor clearColor];
    _moreButton.frame = CGRectMake(0, 0, ScreenWidth, 40);
    _moreButton.titleLabel.font = [UIFont systemFontOfSize:16.0f];
    [_moreButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_moreButton setTitle:@"上拉加载更多..." forState:UIControlStateNormal];
    [_moreButton addTarget:self action:@selector(loadMoreAction) forControlEvents:UIControlEventTouchUpInside];
    
    UIActivityIndicatorView *activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    activityView.frame = CGRectMake(100, 10, 20, 20);
    [activityView stopAnimating];
    activityView.tag = 101;
    
    [_moreButton addSubview:activityView];
    self.tableFooterView = _moreButton;
    
    
}

-(void)setRefreshHeader:(BOOL)refreshHeader{
    
    _refreshHeader = refreshHeader;
    if (_refreshHeader) {
         [self addSubview:_refreshHeaderView];
    }else{
        if([_refreshHeaderView superview]){
            [_refreshHeaderView removeFromSuperview];
        }
    }
}

#pragma mark -action
-(void)loadMoreAction{
    if([self.eventDelegate respondsToSelector:@selector(pullUp:)]){
        [self.eventDelegate pullUp:self];
        [self startLoadMore];
    }


}

-(void)startLoadMore{
    [_moreButton setTitle:@"正在加载..." forState:UIControlStateNormal];
    _moreButton.enabled = NO;
    UIActivityIndicatorView *activityView = (UIActivityIndicatorView *)[_moreButton viewWithTag:101];
    [activityView startAnimating];
}

-(void)stopLoadMore{
    if(self.data.count > 0){
         _moreButton.hidden = NO;
        [_moreButton setTitle:@"上拉加载更多..." forState:UIControlStateNormal];
        _moreButton.enabled = YES;
        UIActivityIndicatorView *activityView = (UIActivityIndicatorView *)[_moreButton viewWithTag:101];
        [activityView stopAnimating];
        
        if(!self.isMore){
            [_moreButton setTitle:@"没有更多了" forState:UIControlStateNormal];
            _moreButton.enabled = NO;
        }
    }else{
        _moreButton.hidden = YES;
    }
    
}

-(void)reloadData{
    [super reloadData];
    [self stopLoadMore];

}


#pragma mark -  tableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.data.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    return [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if([self.eventDelegate respondsToSelector:@selector(tableView: didDeselectRowAtIndexPath:)]){
        [self.eventDelegate tableView:self didDeselectRowAtIndexPath:indexPath];
    }
    

}

#pragma mark Data Source Loading / Reloading Methods

- (void)reloadTableViewDataSource{
    
    //  should be calling your tableviews data source model to reload
    //  put here just for demo
    _reloading = YES;
    
}
//停止加载，下拉回弹
- (void)doneLoadingTableViewData{
    
    _reloading = NO;
    [_refreshHeaderView egoRefreshScrollViewDataSourceDidFinishedLoading:self];
    
}

-(void)refreshData{
    [_refreshHeaderView refreshLoading:self]; 

}


#pragma mark UIScrollViewDelegate Methods
//当滑动时调用的方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    [_refreshHeaderView egoRefreshScrollViewDidScroll:scrollView];
    
}
//停止拖拽的时候调用
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    
    [_refreshHeaderView egoRefreshScrollViewDidEndDragging:scrollView];
    
    if(!self.isMore){
        return;
    }
    
    float offset = scrollView.contentOffset.y;
    float contentHeight = scrollView.contentSize.height;
    float sub = contentHeight-offset;//scrollView的高度
    
    if(scrollView.height - sub > 30){
        [self startLoadMore];
        [self performSelector:@selector(doneLoadingTableViewData) withObject:nil afterDelay:3.0];
        if([self.eventDelegate respondsToSelector:@selector(pullDown:)]){
            [self.eventDelegate pullUp:self];
        }
    }
}


#pragma mark EGORefreshTableHeaderDelegate Methods
//下拉到一定距离，放开后调用的方法
- (void)egoRefreshTableHeaderDidTriggerRefresh:(EGORefreshTableHeaderView*)view{
    //设置为正在加载的状态
    [self reloadTableViewDataSource];
    //停止加载，下拉回弹
    [self performSelector:@selector(doneLoadingTableViewData) withObject:nil afterDelay:3.0];
    if([self.eventDelegate respondsToSelector:@selector(pullDown:)]){
        [self.eventDelegate pullDown:self];
    }

}

- (BOOL)egoRefreshTableHeaderDataSourceIsLoading:(EGORefreshTableHeaderView*)view{
    
    return _reloading; // should return if data source model is reloading
    
}

//获取下拉回弹的时间
- (NSDate*)egoRefreshTableHeaderDataSourceLastUpdated:(EGORefreshTableHeaderView*)view{
    
    return [NSDate date]; // should return date data source was last changed
    
}
@end
